# -*- coding:UTF-8 -*-
import os,re

"""is not None
	判断字符串相等
	字符串连接
"""
def change_extend(dir , befor , after):
	num_change = 0
	if befor[:1] != '.':
		befor = '.' + befor
	if after[:1] != '.':
		after = '.' + after
	for path , subpath , files in os.walk(dir):
		for name in files:
			oldfilename = os.path.join(path,name)
			search_obj = re.search(befor+'$',oldfilename)
			if search_obj is not None:
				newfilename = re.sub(befor+'$',after,oldfilename)
				os.rename(oldfilename,newfilename)
				print oldfilename,' => ', newfilename
				num_change += 1
	print num_change , 'files changed'

if __name__ == '__main__':
	import sys
	if len(sys.argv) < 4:
		print 'Usage:dir befor after'
		sys.exit(100)
	change_extend(sys.argv[1],sys.argv[2],sys.argv[3])
